<template>
  <el-card class="box-card" shadow="always">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="API名称" prop="apiName">
        <el-input v-model="queryParams.apiName" placeholder="请输入API名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="apiName">
        <el-select v-model="showType" class="filter-item" @change="handleExamineList">
          <el-option v-for="item in typeStatus" :key="item.value" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-button size="mini" style="height: 50px;color: #9aaabf" @click="handleExamineList">待审批</el-button>
        <el-button size="mini" style="height: 50px;color: #001528">已审批</el-button> -->

    <el-table v-loading="loading" element-loading-text="正在加载，请稍后" :data="dataApiList" border tooltip-effect="dark" :size="tableSize" :height="tableHeight" style="width: 100%; margin: 15px 0">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="API名称" min-width="150px">
                <template slot-scope="scope">
                    <span>{{ scope.apiName }}</span>
                </template>
            </el-table-column> -->
      <!-- <template v-for="(item, index) in tableColumns">
                <el-table-column v-if="item.show" :key="index" :prop="item.prop" :label="item.label"
                    :formatter="item.formatter" align="center" show-overflow-tooltip />
            </template> -->
      <el-table-column label="API名称" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.apiName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.state | dictLabel('api_examine') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.applyUser }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="applyTime" label="申请时间" :formatter="formatDate" min-width="150px" align="center"> </el-table-column>
      <el-table-column label="审批人" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.examineUser }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="examineTime" label="审批时间" :formatter="formatDate" min-width="150px" align="center"> </el-table-column>
    </el-table>

    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import { pageDataApiExamineHis, examineDataApi } from '@/api/market/dataapi'
import Cookies from 'js-cookie'
import dayjs from 'dayjs'
export default {
  name: 'DataApiExamineList',
  data() {
    return {
      tableHeight: document.body.offsetHeight - 340 + 'px',
      // 展示切换
      showOptions: {
        data: {},
        showList: false,
        showListTab: true,
      },
      // 遮罩层
      loading: true,
      // 表格头
      tableColumns: [
        { prop: 'apiName', label: 'API名称', show: true },

        { prop: 'state', label: '审批状态', show: true },
        { prop: 'applyUser', label: '申请人', show: true },
        { prop: 'applyTime', label: '申请时间', show: true },
        { prop: 'examineUser', label: '审批人', show: true },
        { prop: 'examineTime', label: '审批时间', show: true },
      ],
      // 默认选择中表格头
      checkedTableColumns: [],
      tableSize: 'medium',
      // 状态数据字典
      statusOptions: [],
      // 数据集表格数据
      dataApiList: [],
      // 总数据条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        apiName: '',
        userName: Cookies.get('examinename'),
      },
      typeStatus: [
        {
          label: '通过',
          value: '0',
        },
        {
          label: '未通过',
          value: '1',
        },
        {
          label: '已撤销',
          value: '2',
        },
        {
          label: '审批中',
          value: '3',
        },
      ],
      showType: '已审批',
    }
  },
  created() {
    // getDictDetail('api_examine_state').then(response => {
    //     this.statusOptions = response.content
    // })
    this.getList()
  },
  mounted() {
    this.initCols()
  },
  methods: {
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if (data == null) {
        return null
      }
      let dt = new Date(data)
      return dayjs(dt).format('YYYY-MM-DD HH:DD:ss')
    },
    /** 查询数据Api列表 */
    getList() {
      this.loading = true
      pageDataApiExamineHis(this.queryParams).then((res) => {
        this.loading = false
        if (res.data.code == 200) {
          this.dataApiList = res.data.data.records
          this.total = res.data.data.total
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    initCols() {
      this.checkedTableColumns = this.tableColumns.map((col) => col.prop)
    },
    handleCheckedColsChange(val) {
      this.tableColumns.forEach((col) => {
        if (!this.checkedTableColumns.includes(col.prop)) {
          col.show = false
        } else {
          col.show = true
        }
      })
    },
    handleCommand(command) {
      this.tableSize = command
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        apiName: '',
      }
      this.handleQuery()
    },
    /** 刷新列表 */
    handleRefresh() {
      this.getList()
    },
    /** 审批按钮操作 */
    handleExamine(row) {
      this.$confirm('api接口申请, 是否通过？', '提示', {
        confirmButtonText: '通过',
        cancelButtonText: '驳回',
        type: 'warning',
      })
        .then(() => {
          examineDataApi(row.id, 0).then((response) => {
            if (response.success) {
              this.$message.success('审批成功')
              this.getList()
            }
          })
        })
        .catch(() => {
          examineDataApi(row.id, 1).then((response) => {
            if (response.success) {
              this.$message.success('审批成功')
              this.getList()
            }
          })
        })
    },
    /** tab切换按钮操作 */
    handleExamineList() {
      this.showOptions.data = {}
      this.showOptions.showList = true
      this.showOptions.showListTab = false
      this.$emit('showCard', this.showOptions)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryParams.pageNum = val
      this.getList()
    },
  },
}
</script>

<style lang="scss" scoped>
.right-toolbar {
  float: right;
}

.el-card ::v-deep .el-card__body {
  // height: calc(100vh - 170px);
}
</style>
